package com.kamistoat.meimeichat.meimeichatserverapplication.netty.handlers;

import com.alibaba.fastjson.JSON;
import com.kamistoat.meimeichat.meimeichatserverapplication.netty.NettyServerBizHandler;
import com.kamistoat.meimeichat.meimeichatserverapplication.netty.service.NettyUserService;
import com.kamistoat.meimeichatserveraggrement.protocol.friend.SearchFriendRequest;
import com.kamistoat.meimeichatserveraggrement.protocol.friend.SearchFriendResponse;
import com.kamistoat.meimeichatserveraggrement.protocol.friend.dto.UserDto;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 搜索好友Handler
 */
@Component
@ChannelHandler.Sharable
public class SearchFriendHandlerServer extends NettyServerBizHandler<SearchFriendRequest> {
    @Autowired
    NettyUserService nettyUserService;

    /**
     * @param channel
     * @param searchFriendRequest
     * @Warning:子类只需要重写该方法
     */
    @Override
    public void channelRead(Channel channel, SearchFriendRequest searchFriendRequest) {
        if(searchFriendRequest == null) return;
        logger.info("查询好友请求处理: " + JSON.toJSONString(searchFriendRequest));
        List<UserDto> searchResultList =
                nettyUserService.querySearchFriendList(searchFriendRequest.getUserId(), searchFriendRequest.getSearchKey());
        SearchFriendResponse searchFriendResponse = new SearchFriendResponse();
        searchFriendResponse.setList(searchResultList);
        channel.writeAndFlush(searchFriendResponse);
    }
}
